package com.project.class8_23;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ExecutorTest {

    public static void main(String[] args) {
//        //创建无限扩展的线程池
//        ExecutorService service = Executors.newCachedThreadPool();
//        //向线程池提交任务
//        service.execute(new MyThread());
//        service.execute(new MyThread());
//        //关闭线程池
//        service.shutdown();

        //创建大小为3的线程池
        ExecutorService service = Executors.newFixedThreadPool(2);
        //向线程池提交任务
        service.execute(new MyThread());
        service.execute(new MyThread());

        //关闭线程池
        service.shutdown();

        //创建单线程线程池
//        ExecutorService service = Executors.newSingleThreadExecutor();
//        service.execute(new MyThread());
//        service.execute(new MyThread());
//        service.shutdown();
    }
}

class MyThread implements Runnable{

    @Override
    public void run() {
        for(int i=0;i<10;i++){
            //Thread.currentThread()得到当前线程
            System.out.println(Thread.currentThread()+"   "+i);
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}